<?php

class Loans extends HTech_Db_Table
{
    protected $_name    = 'view_loans';
    protected $_primary = 'LoanId';

    protected $_dependentTables = array('Payments'); 
    
    protected $_referenceMap    = array(
        'Clients' => array(
            'columns'           => array('ClientId'),
            'refTableClass'     => 'Clients',
            'refColumns'        => array('ClientId')
        ),
        'LoanMasters' => array(
            'columns'           => array('LoanMasterId'),
            'refTableClass'     => 'LoanMasters',
            'refColumns'        => array('LoanMasterId')
        ),
        'Applications' => array(
            'columns'           => array('LoanMasterId'),
            'refTableClass'     => 'Applications',
            'refColumns'        => array('LoanMasterId')
        )
    );  
    
    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Loans");
        $this->_gridMetadata['titleMods'] = array('Status','LocationShortName','OfficerUsername','Program','ProductLine');
        $this->_gridMetadata['params'] = array('LoanMasterId','ProductLineId','ProgramId','ClientId', 'LocationId','OfficerUserId','Status','OfficerUsername');
        $this->_gridMetadata['onClick'] = 'loan.loan.view';
        $this->_gridMetadata['headers'] = array
            (
                array('LoanId',               80, $this->translate("Id"),          'Number'),
                array('LoanMasterId',         80, $this->translate("Master Id"),   'Number'),
                array('ClientId',             80, $this->translate("Client Id"),   'Number'),
                array('ClientCode',          100, $this->translate("Client code"), 'String'),
                array('ClientFullName',      180, $this->translate("First name"),  'String'),
                array('GroupName',           250, $this->translate("Group"),       'String'),
                array('BusinessType',        150, $this->translate("Business type"),        'String'),
                array('ProductLine',         150, $this->translate("Line"),        'String'),
                array('ProgramName',         150, $this->translate("Program"),     'String'),
                array('LocationShortName',    80, $this->translate("Location"),    'String'),
                array('OfficerUsername',      80, $this->translate("Officer"),     'String'),
                array('Principal',            80, $this->translate("Principal"),   'Number',   'Standard'),
                array('BalancePrincipal',     80, $this->translate("Bal. prnpal"), 'Number',   'Standard'),
                array('DeliveryDate',         80, $this->translate("Disb. date"), 'DateTime', 'Date'),
                array('CancelDate',           80, $this->translate("Paid date"), 'DateTime', 'Date'),
                array('Status',               80, $this->translate("Status"),      'String')
            );
        return $this;
    }
    
    protected function _setRecordDependentRows()
    {
        $this->_record->r = (object) array('loanStatusChanges' => null, 'payments' => null, 'writeOffCharges' => null);
        $this->_record->r->loanStatusChanges = $this->_record->d->findDependentRowset('LoanStatusChanges');
        $this->_record->r->payments = $this->_record->d->findDependentRowset('Payments');  
        $this->_record->r->writeOffCharges = $this->_record->d->findDependentRowset('LoanWriteOffCharges'); 
        $this->_record->r->writeOffPayments = $this->_record->d->findDependentRowset('LoanWriteOffPayments');     
    }
}